//
//  LPFComposeTextView.swift
//  WeiBo
//
//  Created by mac on 16/4/23.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit

class LPFComposeTextView: UITextView {
    
    // 供外界设置占位文字
    var placeholder: String?{
        didSet{
            placeholderLabel.text = placeholder
        }
    }
    
    // 重写父类已有的属性
    override var font: UIFont?{
        didSet{
            placeholderLabel.font = font
        }
    }

    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // MARK: -  设置视图
    private func setupUI(){
        self.backgroundColor = UIColor.whiteColor()
        
        addSubview(placeholderLabel)
        
        placeholderLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(5)
            make.top.equalTo(8)
            make.width.equalTo(SCREENW - 10)
        }
        
        // 通过通知监听textView 文字改变
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "textViewTextDidChange", name: UITextViewTextDidChangeNotification, object: nil)
        
        
    }
    // 监听文字改变 隐藏占位问题
    @objc private func textViewTextDidChange(){
        placeholderLabel.hidden = self.hasText()
    }
    
    
    // MARK: - 提示控件
    private lazy var placeholderLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFontOfSize(12)
        label.textColor = UIColor.grayColor()
        label.numberOfLines = 0
        
        return label
    }()
}
